package gdut.imis.controller;

import gdut.imis.Utils.Constants;
import gdut.imis.Utils.Result;
import gdut.imis.enums.Status;

import java.text.MessageFormat;
import java.util.Map;

/**
 * @Description base controller 基类
 * @Author JiaFu
 * @Date 2023/4/7 11:52
 */
public class BaseController {

    /***
     * @param 检查前端传入参赛pageNo,pageSize有效性
     * @param: pageNo
     * @param: pageSize
     * @return Result
     */
    public Result checkPageParams(int pageNo, int pageSize) {
        Result result = new Result();
        Status resEnum = Status.SUCCESS;
        String msg = resEnum.getMsg();
        if (pageNo <= 0) {
            resEnum = Status.REQUEST_PARAMS_NOT_VALID_ERROR;
            msg = MessageFormat.format(resEnum.getMsg(), Constants.PAGE_NUMBER);
        } else if (pageSize <= 0) {
            resEnum = Status.REQUEST_PARAMS_NOT_VALID_ERROR;
            msg = MessageFormat.format(resEnum.getMsg(), Constants.PAGE_SIZE);
        }
        result.setCode(resEnum.getCode());
        result.setMsg(msg);
        return result;
    }

    public Result resturnDataList(Map<String, Object> result) {
        Status status = (Status) result.get(Constants.STATUS);
        if (status == Status.SUCCESS) {
            String msg = Status.SUCCESS.getMsg();
            Object dataList = result.get(Constants.DATA_LIST);
            return success(msg, dataList);
        }else {
            int code = status.getCode();
            String msg = (String) result.get(Constants.MSG);
            return error(code,msg);
        }
    }

    public Result success() {
        Result result = new Result();
        result.setCode(Status.SUCCESS.getCode());
        result.setMsg(Status.SUCCESS.getMsg());
        return result;
    }

    public Result success(String msg) {
        Result result = new Result();
        result.setCode(Status.SUCCESS.getCode());
        result.setMsg(msg);
        return result;
    }

    public Result success(String msg, Object list) {
        Result result = new Result();
        result.setCode(Status.SUCCESS.getCode());
        result.setMsg(msg);
        result.setData(list);
        return result;
    }

    public Result success(Object list) {
        Result result = new Result();
        result.setCode(Status.SUCCESS.getCode());
        result.setMsg(Status.SUCCESS.getMsg());
        result.setData(list);
        return result;
    }

    public Result error(int code,String msg){
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
}
